﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Collections;

namespace BestClient
{
    public class DataBuffer{

        //private const static string TAG = DataBuffer.class.getCanonicalName();
        private Dictionary<string, SendData> sendMap = new Dictionary<string, SendData>();
        //private ClientMessageInteract client;
        private SettingsProvider instance;


        class SendData {
            public string mid;
            public string data;

            public SendData(string mid, string data) {

                this.data = data;
                this.mid = mid;
            }
        }

        //public DataBuffer(ClientMessageInteract client, Context context) {
        //    this.client = client;
        //    instance = SettingsProvider.getInstance();
        //}

        public void clearCache() {
            if (null != sendMap) {
                sendMap.Clear();
            }
        }

        public bool receiveDataAndDel(string mid) {
            bool success = true;
            if (sendMap.ContainsKey(mid))
            {
                SendData sendData = sendMap[mid];
                if (null != sendData)
                {
                    //Log.d(TAG, "delSendDataFromBuff remove mid:" + mid);
                    success = true;
                }
                else
                {
                    success = false;
                    //Log.d(TAG, "delSendDataFromBuff no data for mid:"
                    //        + mid);
                }
                sendMap.Remove(mid);
            }
            return success;
        }

        public string getMessage(string mid) {
            if (sendMap[mid] != null) {
                return sendMap[mid].data;
            } else {
                throw new Exception("can find data through %s" + mid);
            }
        }

        public void addSendData2Buff(string mid, string data) {
            if (!sendMap.ContainsKey(mid))
            {
                return;
            }
            //if (sendMap[mid] != null) {
            //    //Log.i(TAG, "repeat  package not to add the cache");
            //    return;
            //}
            if (sendMap.Count() < 5000) {
                SendData sendData = new SendData(mid, data);
                sendMap.Add(mid, sendData);
                //Log.i(TAG, "mid:" + mid + "  add to cache " + " data: (" + data + " )");
            }
        }

        //@Override
        public void Run() 
        {
            while (true) {
                try {
                
                    Thread.Sleep(20000);
                } catch (Exception e) {
                    //Thread.currentThread().interrupt();
                    //Log.e(TAG, Log.getStackTracestring(e));
                }
                
                foreach (KeyValuePair<string, SendData> entry in sendMap) {
                    string mid = entry.Key;
                    SendData sendData = entry.Value;
                    //Log.d(TAG, "run send  again mid:" + mid);
                    // resend data
                    BestData bestData = MessageConverter.fromJson<BestData>(sendData.data);
                    sendMap.Remove(bestData.mid);
                    Random random = new Random();
                    string newMid = random.NextDouble().ToString();
                    bestData.mid = newMid;
                    if (string.Equals(bestData.sid, BestConstant.SERVICE_ID_REGISTER)) {
                        bestData.setPayloadParam("did", MessageConverter.generateDid(newMid));
                    } else if (string.Equals(bestData.sid, BestConstant.SERVICE_ID_UPLOAD_COUNT) ||
                            string.Equals(bestData.sid, BestConstant.SERVICE_ID_UPLOAD_RECOGNITION) ||
                            string.Equals(bestData.sid, BestConstant.SERVICE_ID_UPLOAD_ALARM_LOG) ||
                            string.Equals(bestData.sid, BestConstant.SERVICE_ID_UPLOAD_PHOTO)) {
                        string timeStamp = (string) instance.get(bestData.mid, null);
                        if (timeStamp != null) {
                            instance.Remove(bestData.mid);
                            instance.set(newMid, timeStamp);
                        }
                    }
                    string newSendData = MessageConverter.toJson(bestData);
                    //client.Send(newSendData);
                    Console.WriteLine("DZ_send data again data:( " + newSendData + " )");
                }
            }
        }
    }
}
